package es.ats.codington.web.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.EventSignUpService;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class UnregisterToEventServlet
 */
@WebServlet("/UnregisterToEventServlet")
public class UnregisterToEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UnregisterToEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	/**
	 * Method invoked when the visitor wants to unregister for an event
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		int idVisitor = 0;
		 int idEvent1 = 0;
        
		HttpSession session = request.getSession(true);
		
		EventSignUpService service = new EventSignUpService();
		EventSignUp eventsu = new EventSignUp();
		EventService eservice = new EventService();
		Event ev = new Event();
		
		String n=(String) session.getAttribute("username");
		
        VisitorService vservice = new VisitorService();
		
		Visitor v = new Visitor();
		v.setUserName(n);
		
		idVisitor = vservice.getIdVisitor(v);
		/**
		 * Get the id of event
		 */
		idEvent1 = Integer.parseInt(request.getParameter("idEventMy"));
		
		eventsu.setIdVisitor(idVisitor);
		eventsu.setIdEvent(idEvent1);
		
		ev.setEventID(idEvent1);
		
		/**
    	 * The visitor is unregistered in the event
    	 */
	    service.unregisterVisitorToEvent(eventsu);
	    /**
    	 * Available seats increment event
    	 */
	    eservice.updateSeatsEventInc(ev);
	    /**
		 * Invokes the action "PersonalServlet"
		 */
	    response.sendRedirect("PersonalServlet");
	   
	}

}
